//
//  xesApp.m
//  AFNetworking
//
//  Created by tianlong on 2018/9/24.
//

#import "xesApp.h"
#import "WXAuthorizationTools.h"

@interface xesApp ()
@property (nonatomic, strong) WXAuthorizationTools *tools;
@end

@implementation xesApp

- (WXAuthorizationTools *)tools  API_AVAILABLE(ios(8.0)){
    if (!_tools) {
        _tools = [[WXAuthorizationTools alloc] init];
    }
    return _tools;
}

/**
 相机：201
 录音：202
 手机状态：203
 定位：204
 本地存储：205(android)
 相册：206
 日历：207
 回调给JS的状态： 1有权限 2无权限
 */
- (void)checkPermission:(id)params {
    NSArray *curParams = [JSContext currentArguments];
    int permissionType    = -1;
    NSString *callBackMethod    = nil;
    
    for(int i = 0; i < curParams.count; i++) {
        JSValue *value = [curParams objectAtIndex:i];
        if(i==0){
            permissionType = [value toInt32];
        }else if (i==1){
            callBackMethod = [value toString];
        }
    }
    
    if (permissionType == -1) {
//        [UMSAgentUtil postEventId:@"xesApp" attachment:@"permissionType==-1"];
        return;
    }
    

    switch (permissionType) {
        case 201://相机
        {
            [WXAuthorizationTools checkAndRequestAccessForType:WXPrivacyType_Camera accessStatus:^(WXAuthorizationStatus status, WXPrivacyType type) {
                [self callJSWithFuntionName:callBackMethod isAuthorized:status == WXAuthorizationStatus_Authorized];
            }];
        }
            break;
        case 202://录音
        {
            [WXAuthorizationTools checkAndRequestAccessForType:WXPrivacyType_Microphone accessStatus:^(WXAuthorizationStatus status, WXPrivacyType type) {
                [self callJSWithFuntionName:callBackMethod isAuthorized:status == WXAuthorizationStatus_Authorized];
            }];
        }
            break;
        case 203://手机状态
            break;
        case 204://定位
        {
            [self.tools checkAndRequestAccessForLocationServicesWithAccessStatus:^(WXLocationAuthorizationStatus status) {
                if (status == WXLocationAuthorizationStatus_Authorized || //一直允许获取定位 ps：< iOS8用
                    status == WXLocationAuthorizationStatus_AuthorizedAlways || //一直允许获取定位
                    status ==WXLocationAuthorizationStatus_AuthorizedWhenInUse) {//在使用时允许获取定位
                    [self callJSWithFuntionName:callBackMethod isAuthorized:YES];
                }else {
                    [self callJSWithFuntionName:callBackMethod isAuthorized:NO];
                }
            }];
        }
            break;
        case 205://本地存储
            break;
            
        case 206://相册
        {
            [WXAuthorizationTools checkAndRequestAccessForType:WXPrivacyType_Photos accessStatus:^(WXAuthorizationStatus status, WXPrivacyType type) {
                [self callJSWithFuntionName:callBackMethod isAuthorized:status == WXAuthorizationStatus_Authorized];
            }];
        }
            break;
            
        case 207://日历
        {
            [WXAuthorizationTools checkAndRequestAccessForType:WXPrivacyType_Calendars accessStatus:^(WXAuthorizationStatus status, WXPrivacyType type) {
                [self callJSWithFuntionName:callBackMethod isAuthorized:status == WXAuthorizationStatus_Authorized];
            }];
        }
            break;
            
        default:
            break;
    }
}

- (void)callJSWithFuntionName:(NSString *)fun isAuthorized:(BOOL)isAuthorized {
    if (isAuthorized) {
        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"%@(%d)",fun,(1)]];
    }else {
        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"%@(%d)",fun,(2)]];
    }
}

- (void)changeNavgationBarTitle:(id)params {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"%s__params=%@",__FUNCTION__,params);
        if ([self.delegate respondsToSelector:@selector(changeNavgationBarTitle:)]) {
            [self.delegate changeNavgationBarTitle:params];
        }
    });
}

- (void)changeNavgationBarBackgroundColor:(id)params {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"%s__params=%@",__FUNCTION__,params);
        if ([self.delegate respondsToSelector:@selector(changeNavgationBarBackgroundColor:)]) {
            [self.delegate changeNavgationBarBackgroundColor:params];
        }
    });
}

- (void)showNavgationBarRightButton:(id)params {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"");
        if([self.delegate respondsToSelector:@selector(showNavgationBarRightButton:)]) {
            [self.delegate showNavgationBarRightButton:params];
        }
    });
}

- (void)changeTitleColor:(id)params {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"");
        if([self.delegate respondsToSelector:@selector(changeTitleColor:)]) {
            [self.delegate changeTitleColor:params];
        }
    });
}

@end
